package com.lib.framework.widget.dialog

import android.content.Context
import android.text.TextUtils
import android.view.View
import androidx.annotation.ColorInt
import androidx.databinding.DataBindingUtil
import com.lib.framework.R
import com.lib.framework.databinding.WidgetOperatePromptDialogBinding

/**
 * 操作提示 对话框
 *
 * @author keanbin
 */
class OperatePromptDialog(context: Context) : BaseDialog(context, R.style.defaultDialogTheme03) {

    private var mDataBinding: WidgetOperatePromptDialogBinding? = null

    private var mOnClickConfirmBtnListener: OnClickConfirmBtnListener? = null

    private var mOnClickCancelBtnListener: OnClickCancelBtnListener? = null

    init {
        initView()

        setCanceledOnTouchOutside(false)
        setCancelable(false)
    }

    private fun initView() {
        mDataBinding = DataBindingUtil.inflate(
            layoutInflater,
            R.layout.widget_operate_prompt_dialog,
            null,
            false
        )
        mDataBinding?.let { dataBinding ->
            setContentView(dataBinding.root)

            dataBinding.ivClose.setOnClickListener {
                cancel()
            }

            dataBinding.tvCancel.setOnClickListener {
                if (mOnClickCancelBtnListener == null) {
                    cancel()
                } else {
                    mOnClickCancelBtnListener?.onClickCancelBtn()
                }
            }

            setOnCancelListener {
            }

            dataBinding.tvConfirm.setOnClickListener {
                mOnClickConfirmBtnListener?.onClickConfirmBtn()
            }
        }
    }

    /**
     * 设置标题
     */
    fun setTitleResId(resId: Int) {
        setTitle(mContext.getString(resId))
    }

    /**
     * 设置标题
     */
    fun setTitle(str: String?) {
        if (TextUtils.isEmpty(str)) {
            mDataBinding?.tvTitle?.text = ""
            mDataBinding?.tvTitle?.visibility = View.GONE
        } else {
            mDataBinding?.tvTitle?.text = str
            mDataBinding?.tvTitle?.visibility = View.VISIBLE
        }
    }

    /**
     * 设置内容
     */
    fun setContentResId(resId: Int) {
        setContent(mContext.getString(resId))
    }

    /**
     * 设置内容
     */
    fun setContent(str: String) {
        mDataBinding?.tvContent?.text = str
    }

    /**
     * 设置副内容
     */
    fun setSubContent(str: String?) {
        if (TextUtils.isEmpty(str)) {
            mDataBinding?.tvSubContent?.text = ""
            mDataBinding?.tvSubContent?.visibility = View.GONE
        } else {
            mDataBinding?.tvSubContent?.text = str
            mDataBinding?.tvSubContent?.visibility = View.VISIBLE
        }
    }

    /**
     * 设置确认按钮文本
     */
    fun setConfirmBtnTextResId(resId: Int) {
        setConfirmBtnText(mContext.getString(resId))
    }

    /**
     * 设置确认按钮文本
     */
    fun setConfirmBtnText(str: String) {
        mDataBinding?.tvConfirm?.text = str
    }

    /**
     * 设置取消按钮文本
     */
    fun setCancelBtnTextResId(resId: Int) {
        setCancelBtnText(mContext.getString(resId))
    }

    /**
     * 设置取消按钮文本
     */
    fun setCancelBtnText(str: String) {
        mDataBinding?.tvCancel?.text = str
    }

    /**
     * 设置取消按钮文本颜色
     */
    fun setCancelBtnTextColor(@ColorInt color: Int) {
        mDataBinding?.tvCancel?.setTextColor(color)
    }

    /**
     * 设置确认钮文本颜色
     */
    fun setConfirmBtnTextColor(@ColorInt color: Int) {
        mDataBinding?.tvConfirm?.setTextColor(color)
    }

    /**
     * 设置 确定按钮点击监听器
     */
    fun setOnClickConfirmBtnListener(l: OnClickConfirmBtnListener) {
        mOnClickConfirmBtnListener = l
    }

    /**
     * 设置 取消按钮点击监听器
     */
    fun setOnClickCancelBtnListener(l: OnClickCancelBtnListener) {
        mOnClickCancelBtnListener = l
    }

    /**
     * 显示 关闭 图标
     */
    fun showCloseIcon(isShow: Boolean) {
        mDataBinding?.ivClose?.visibility = if (isShow) View.VISIBLE else View.GONE
    }

    /**
     * 是否显示确认按钮，默认显示
     */
    fun showConfirmBtn(isShow: Boolean) {
        mDataBinding?.vLine04?.visibility = if (isShow) View.VISIBLE else View.GONE
        mDataBinding?.tvConfirm?.visibility = if (isShow) View.VISIBLE else View.GONE
    }

    /**
     * 确定按钮点击监听器
     */
    interface OnClickConfirmBtnListener {

        /**
         * 确定按钮点击 回调
         */
        fun onClickConfirmBtn()
    }

    /**
     * 取消按钮点击监听器
     */
    interface OnClickCancelBtnListener {

        /**
         * 取消按钮点击 回调
         */
        fun onClickCancelBtn()
    }
}